package com.granada.controller.sys;

import com.granada.behaviour.neutral.PermissionWithNeutralBehavior;
import com.granada.behaviour.neutral.RoleWithNeutralBehavior;
import com.granada.behaviour.pc.UserAuthWithPcBehavior;
import com.granada.entity.SysUser;
import com.granada.service.base.PermissionService;
import com.granada.service.base.UserAuthService;
import com.granada.service.pc.UserAuthWithPcService;
import com.granada.service.ping.PermissionWithPingService;
import com.granada.utils.DataResult;
import com.granada.utils.JwtTokenUtil;
import com.granada.aop.annotation.LogAnnotation;
import com.granada.model.pc.PermissionWithPing;
import com.granada.utils.MapperBeanUtils;
import com.granada.vo.req.LoginReqVO;
import com.granada.vo.resp.HomeRespVO;
import com.granada.vo.resp.LoginRespVO;
import com.granada.vo.resp.PermissionRespNode;
import com.granada.vo.resp.UserInfoRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/sys")
@Api(tags = "小程序-建议管理")
public class SysUserAuthController {
    @PostMapping(value = "/user/login")
    @ApiOperation(value = "用户登录接口")
    @LogAnnotation(title = "用户登录", action = "用户登录")
    public DataResult<LoginRespVO> login(@RequestBody @Valid LoginReqVO vo) throws Exception{
        DataResult<LoginRespVO> result= DataResult.success();

        UserAuthService userAuthService = new UserAuthWithPcService(
                new UserAuthWithPcBehavior(),
                new PermissionWithNeutralBehavior(),
                new RoleWithNeutralBehavior()
        );
        LoginRespVO login = userAuthService.login(vo);
        if (login==null){
            result.setMsg("该账号有效时间已过，请联系平台");
        }else {
            result.setData(login);
        }

        return result;
    }

    @PostMapping("/home")
    @ApiOperation(value ="获取首页数据接口")
    @RequiresPermissions("sys:home")
    public DataResult<HomeRespVO> getHomeInfo(HttpServletRequest request) throws Exception {
        String accessToken=request.getHeader("authorization");
        Long userId= JwtTokenUtil.getUserId(accessToken);
        SysUser sysUser = MapperBeanUtils.getSysUserMapper().selectByPrimaryKey(userId);
        UserInfoRespVO vo = UserInfoRespVO.builder().build();

        PermissionService permissionService = new PermissionWithPingService(
                new PermissionWithPing(
                        null,
                        null,
                        null
                ),
                new PermissionWithNeutralBehavior()
        );
        List<PermissionRespNode> menus = permissionService.permissionTreeList(userId);
        HomeRespVO respVO = HomeRespVO.builder()
                .menus(menus)
                .userInfo(vo)
                .build();

        DataResult<HomeRespVO> result=DataResult.success();
        result.setData(respVO);
        return result;
    }

}
